import json
from pyecharts.charts import Map
from pyecharts.options import *

# def province(pro):
#     provinces = ["北京市", "天津市", "河北省", "山西省", "内蒙古自治区", "辽宁省", "吉林省", "黑龙江省", "上海市",
#                  "江苏省", "浙江省", "安徽省", "福建省", "江西省",
#                  "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西壮族自治区", "海南省", "重庆市", "四川省",
#                  "贵州省", "云南省", "西藏自治区", "陕西省", "甘肃省",
#                  "青海省", "宁夏回族自治区", "新疆维吾尔自治区", "台湾省", "香港特别行政区", "澳门特别行政区"]
#
#     # for p in provinces:
#     #     if p.find(pro) >= 0:
#     #         return p
#     #         break
#
#     for p in provinces:
#         if pro in p:
#             return p
#         # break


# 读取数据文件
f = open("D:/疫情.txt", "r", encoding="UTF-8")
data = f.read()  # 全部数据

data_dict = json.loads(data)  # 基础数据字典

province_data_list = data_dict["areaTree"][0]["children"]

data_list = []  # 绘图需要用的数据列表
for province_data in province_data_list:
    province_name = province_data["name"]  # 省份名称
    if province_name == "新疆":
        province_name = "新疆维吾尔自治区"
    elif province_name == "广西":
        province_name = "广西壮族自治区"
    elif province_name == "宁夏":
        province_name = "宁夏回族自治区"
    elif province_name in ["内蒙古", "西藏"]:
        province_name = province_name + "自治区"
    elif province_name in ["北京", "天津", "重庆", "上海"]:
        province_name = province_name + "市"
    elif province_name in ["香港", "澳门"]:
        province_name = province_name + "特别行政区"
    else:
        province_name = province_name + "省"

    province_confirm = province_data["total"]["confirm"]  # 确诊人数
    data_list.append((province_name, province_confirm))

# 创建地图对象
map = Map()
# 添加数据
map.add("各省份确诊人数", data_list, "china")
# 设置全局配置，定制分段的视觉映射
map.set_global_opts(
    title_opts=TitleOpts(title="全国疫情地图"),
    visualmap_opts=VisualMapOpts(
        is_show=True,  # 是否显示
        is_piecewise=True,  # 是否分段
        pieces=[
            {"min": 1, "max": 99, "lable": "1~99人", "color": "#CCFFFF"},
            {"min": 100, "max": 999, "lable": "100~9999人", "color": "#FFFF99"},
            {"min": 1000, "max": 4999, "lable": "1000~4999人", "color": "#FF9966"},
            {"min": 5000, "max": 9999, "lable": "5000~99999人", "color": "#FF6666"},
            {"min": 10000, "max": 99999, "lable": "10000~99999人", "color": "#CC3333"},
            {"min": 100000, "lable": "100000+", "color": "#990033"},
        ]
    )
)
# 绘图
map.render("全国疫情地图.html")
f.close()
